package com.zcx.wxapi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zcx.R;
import com.zcx.base.BaseActivity;
import com.zcx.bean.PayBean;
import com.zcx.bean.WxPayBean;
import com.zcx.common.DialogHelper;
import com.zcx.common.Helper;
import com.zcx.common.ProgressDialog;
import com.zcx.presenter.PayPresenter;
import com.zcx.ui.WxPayActivity;
import com.zcx.util.Constants;
import com.zcx.util.LogUtil;
import com.zcx.view.AlipayView;

import java.util.Map;

public class WXPayEntryActivity extends BaseActivity<AlipayView,PayPresenter> implements AlipayView,IWXAPIEventHandler {

	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	private ProgressDialog progressDialog;
	private Map<String, String> result;
	private static final String ALI_PAY_SUCCESS = "9000";
    private IWXAPI api;

	@Override
	protected PayPresenter setPresenter() {
		return new PayPresenter();
	}

	@Override
	protected void onCreateView(Bundle savedInstanceState) {
		setContentView(R.layout.activity_wx_pay);
		api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
		api.handleIntent(getIntent(), this);
		findViewById(R.id.btnPay).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				getBasePresenter().wxPay();
			}
		});
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		LogUtil.d(TAG, "onPayFinish, errCode = " + resp.errCode);
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.app_tip);
			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
			builder.show();
		}
	}

	@Override
	public void startPay(int resId) {
		progressDialog = DialogHelper.showDialog(this, progressDialog, resId);
	}

	@Override
	public void getPaySucc(PayBean payBean) {
	}

	@Override
	public void getPayFail(String message) {
		Toast.makeText(WXPayEntryActivity.this, message, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void getWxPaySucc(WxPayBean payBean) {
		DialogHelper.dismissDialog(progressDialog);
		if (Helper.isNull(payBean)) {
			return;
		}
		// 开始支付
		startWechatPay(payBean);
	}

	private void startWechatPay(WxPayBean payBean) {
//		PayReq payReq = new PayReq();
//		payReq.appId = getString(R.string.APP_ID);
////		payReq.partnerId = payBean.getPartnerid();
//		payReq.prepayId = payBean.getPrepay_id();
//		payReq.nonceStr = payBean.getNonce_str();
////		payReq.timeStamp = payBean.getTimestamp();
////		payReq.packageValue = payBean.getPk();
//		payReq.sign = payBean.getSign();
//		api.sendReq(payReq);
		PayReq request = new PayReq();
		request.appId = getString(R.string.APP_ID);
		request.partnerId = "1900000109";
		request.prepayId= "1101000000140415649af9fc314aa427";
		request.packageValue = "Sign=WXPay";
		request.nonceStr= "1101000000140429eb40476f8896f4c9";
		request.timeStamp= "1398746574";
		request.sign= "7FFECB600D7157C5AA49810D2D8F28BC2811827B";
		api.sendReq(request);

	}
}